package com.tuanzi.loan.business.repository.common.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.repository.common.custom.CommProductChannelRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.channel.ChannelSearchRequest;
import com.tuanzi.loan.vo.response.channel.ChannelSearchResponse;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;

public class CommProductChannelRepositoryImpl extends JOOQBaseRepository implements CommProductChannelRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return COMM_PRODUCT_CHANNEL;
    }

    public Integer checkRepeatChannelCode(CommProductChannel channel) {
        Condition condition = DSL.trueCondition();
        condition = condition.and(COMM_PRODUCT_CHANNEL.CHANNEL_CODE.eq(channel.getChannelCode()));
        if (channel.getId() != null && channel.getId() != 0) {
            condition = condition.and(COMM_PRODUCT_CHANNEL.ID.ne(channel.getId()));
        }
        SelectQuery sql = dslContext.selectCount().from(COMM_PRODUCT_CHANNEL).where(condition).getQuery();
        Integer count = (Integer) sql.fetchOne(0, Integer.class);
        return count;
    }

    @Override
    public Integer checkDuplicatedChannelCode(Integer startChannelCode, Integer endChannelCode) {
        List<Integer> list = Lists.newArrayList();
        for (int i = startChannelCode; i < endChannelCode; i++) {
            list.add(i);
        }
        return dslContext.selectCount().from(COMM_PRODUCT_CHANNEL).where(COMM_PRODUCT_CHANNEL.CHANNEL_CODE.in(list)).fetchOneInto(Integer.class);
    }

    @Override
    public Page<ChannelSearchResponse> list(ChannelSearchRequest searchRequest, Pageable pageable) {

        fields(COMM_PRODUCT_CHANNEL.fields());
        fields(COMM_PRODUCT_PRDID.NAME);

        leftJoin(COMM_PRODUCT_PRDID, COMM_PRODUCT_PRDID.PRDID.eq(COMM_PRODUCT_CHANNEL.PRDID));

        contains(COMM_PRODUCT_CHANNEL.CHANNEL_NAME, searchRequest.getChannelName());
        eq(COMM_PRODUCT_CHANNEL.PRDID, searchRequest.getProductId());
        eq(COMM_PRODUCT_CHANNEL.CHANNEL_CODE, searchRequest.getChannelCode());
        eq(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP, searchRequest.getChannelGroup());
        eq(COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP, searchRequest.getSecondaryChannelGroup());
        return search(pageable, new ChannelSearchResponseRecordMapper());
    }

    private final class ChannelSearchResponseRecordMapper implements RecordMapper<Record, ChannelSearchResponse> {

        @Override
        public ChannelSearchResponse map(Record record) {
            ChannelSearchResponse response = new ChannelSearchResponse();
            response.setId(record.get(COMM_PRODUCT_CHANNEL.ID));
            response.setChannelCode(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_CODE));
            response.setChannelName(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_NAME));
            response.setChannelGroup(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP));
            response.setProductName(record.get(COMM_PRODUCT_PRDID.NAME));
            response.setSecondaryChannelGroup(record.get(COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP));
            return response;
        }
    }

}
